package sale.wxb.loveshopping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import sale.wxb.loveshopping.entity.convert.SaveMenuVoToSysMenu;
import sale.wxb.loveshopping.entity.vo.common.PageRecord;
import sale.wxb.loveshopping.entity.vo.menu.SaveMenuVo;
import sale.wxb.loveshopping.entity.model.SysMenu;
import sale.wxb.loveshopping.dao.SysMenuDao;
import sale.wxb.loveshopping.entity.vo.common.PageListParam;
import sale.wxb.loveshopping.service.ISysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sale.wxb.loveshopping.utils.PageRecordUtil;

import java.util.List;

/**
 * <p>
 * 菜单资源表 服务实现类
 * </p>
 *
 * @author 王小宝
 * @since 2023-01-13
 */
@Transactional
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenu> implements ISysMenuService {

    @Override
    public Long saveMenu(SaveMenuVo saveMenuVo) {
        SysMenu sysMenu = SaveMenuVoToSysMenu.INSTANCE.sourceToTarget(saveMenuVo);
        sysMenu.setId(sysMenu.getId() == null || sysMenu.getId() == 0 ? null : sysMenu.getId());
        saveOrUpdate(sysMenu);
        return sysMenu.getId();
    }

    @Override
    public List<SysMenu> hasChildrenMenuById(Long id) {
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMenu::getParentId, id);
        return this.list(queryWrapper);
    }

    @Override
    public PageRecord<SysMenu> menuListByPage(PageListParam param) {
        Page<SysMenu> sysMenuPage = new Page<>(param.getPage(), param.getSize());
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(param.getKeyword()), SysMenu::getName, param.getKeyword());
        return PageRecordUtil.toPageRecord(this.page(sysMenuPage, queryWrapper));
    }
}
